<?php

/**
 * 用户关注
 */

namespace app\api\model;


use app\common\lib\RespCode;
use custom\Token;
use PDOStatement;
use think\Collection;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\exception\PDOException;
use think\Model;

class UserAttention {

    /**
     * 添加用户关注
     * @param $params
     * @return int|string
     * @throws Exception
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function add($params) {
        if ($params['r_type'] == 2) throw new Exception('未开放小区关注');
        $house = Db::name("house")
            ->where('is_shelf', 1)
            ->find($params['id']);
        if (empty($house)) throw new Exception('房源已下架');
        $user_attention = Db::name("user_attention")
            ->where('pure_id', $params['id'])
            ->where('user_id', $params['user_id'])
            ->find();
        if (!empty($user_attention)) throw new Exception('请勿重复添加');
        if (db('user_attention')
                ->where('user_id', $params['user_id'])
                ->count() >= 10) {
            throw new Exception('最多添加10个关注');
        }
        $insert = [
            'user_id' => $params['user_id'],
            'pure_id' => $params['id'],
            'pure_type' => 1,
            'itime' => time(),
        ];
        $res = Db::name('user_attention')->insert($insert);
        if (!$res) {
            throw new Exception('操作失败');
        }
        return true;
    }

    /**
     * 删除关注
     * @param $params
     * @return int
     * @throws Exception
     * @throws PDOException
     */
    public static function del($params) {
        $ids = $params['ids'];
        $ids = explode(',', $ids);
        foreach ($ids as $k => $v) {
            Db::name('user_attention')->where('user_id', $params['user_id'])->where('pure_id', $v)->delete();
        }
    }

    /**
     * 关注房源
     * @param $params
     * @return array|PDOStatement|string|Collection
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function show($params) {
        if ($params['r_type'] == 1) {
            $map = [];
            if (!empty($params['title'])) {
                $map[] = ['h.title', 'like', '%' . trim($params['title']) . '%'];
            }
            $House = new House();
            $column = array_merge($House->houseFields, $House->villageFields, $House->typeFields, $House->auctionFields,$House->businessFields,$House->reginFields);
            $pure_id = Db::name('user_attention')
                ->where('user_id', $params['user_id'])
                ->where('pure_type', 1)
                ->order('id', 'desc')
                ->column('pure_id');
            $map[] = ['h.id', 'in', $pure_id];
            $pure_id_str = join(',', $pure_id);
            $res = $House->house_list($map, '', $column, 1, 10, [], $pure_id_str)[0];
            return House::house_list_style($res);
        } else {
            return [];
        }
    }

    /**
     * 用户是否关注这个房源
     * @param $params
     * @return array|PDOStatement|string|Model|null
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function isAttention($params) {
        return Db::name('user_attention')
            ->where('pure_type', 1)
            ->where('user_id', $params['user_id'])
            ->where('pure_id', $params['id'])
            ->find();
    }

}
